<?php
namespace Home\Controller;
 use Think\Controller;
/**
 * 广告控制器
 */
class AdController extends HomeController{
    /**
     * 广告列表控制器
     */
    public function index()
    {

        $page=I('get.page',1,'strip_tags');
       $count=M('advertisement')->count();
     $pagearr= getPageArr($page,C('PAGESIZE'),$count);
     $result=M('advertisement')->limit($pagearr['start'],$pagearr['pagesize'])->select();
       $this->assign('res',$result)->assign('pagearr',$pagearr);
       $this->display();
    }
    /**
     * 添加广告
     */
    public function append()
    {
      if($_POST)
      {////保存广告st
      $data['advname']=I('post.advname','','strip_tags');
        $config['maxsize']=C('IMG_MAX_SIZE');
         $config['path']=C('Admin_IMG_PATH');
         $img=imgUpload($config);
         $advimg=$img['advimg'];
         
           if(!empty($advimg['savename']))
        {
            $data['advimg']=substr(C('Admin_IMG_PATH'),1).$advimg['savepath'].$advimg['savename'];
        }
      
        $id=M('advertisement')->add($data);

        if($id>0)
        {
            echo "<script>alert('广告添加成功');window.location.href='./index'</script>";
        }else
        {
          $this->error("操作失败");
        }
        /////保存广告end

      }else{
          $this->display();
        }
    }
    /**
     * 删除广告
     */
    public function del()
    {       $where['id']=I('get.id','','strip_tags');
           $re=M('advertisement')->where($where)->delete();
          
           if($re!==false)
           {
               echo '1';///删除成功
               return '';
           }
        echo '2';///删除失败
    }
    /**
     * 广告编辑
     */
    public function  edit()
    {  
        if($_POST)
        {
        /////广告修改保存st
          $where['id']=I('post.id','','strip_tags');
          $data['advname']=I('post.advname','','strip_tags');
            if(!empty($_FILES['advimg']['tmp_name'])){
          $config['maxsize']=C('IMG_MAX_SIZE');
         $config['path']=C('Admin_IMG_PATH');
         $img=imgUpload($config);
         $advimg=$img['advimg'];

          $data['advimg']=substr(C('Admin_IMG_PATH'),1).$advimg['savepath'].$advimg['savename'];


    }
        $re=M('advertisement')->where($where)->data($data)->save();
        if($re!==false)
        {
             echo "<script>alert('广告修改成功');window.location.href='./index'</script>";
        }else
        {
             $this->error("操作失败");
        }
        ////广告修改保存end

        }else
        {////广告修改静态页st
           $where['id']=I('get.id','','strip_tags');
          $result=M('advertisement')->where($where)->find();
            $this->assign('res',$result);
           $this->display(); 
           ////广告修改静态页end 
        }


    }


      
   

}